//
//  SearchViewControl.swift.swift
//  DemoUI
//s 搜索栏控制器
//  Created by 白瑾浩 on 2022/1/7.
//

import Foundation
import UIKit

class SearchViewControl : UIViewController,UISearchBarDelegate{
    var data:String?
    var name:String?
    //构造方法传值
    init(data:String){
        name = data
        super.init(nibName: nil, bundle: nil)
    }
    //可选构造函数
//    init?(data:String){
//        name = data
//        super.init(nibName: nil, bundle: nil)
//    }
    required init?(coder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: coder)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        
        let mUIButton = UIButton(frame: CGRect(x: 20, y: 80, width: 300, height: 30) )
        mUIButton.setTitle("返回-\(data!)-\(name!)", for: UIControl.State.normal)
        mUIButton.setTitleColor(UIColor.red, for: UIControl.State.normal)
        mUIButton.addTarget(self, action: #selector(back), for: UIControl.Event.touchUpInside)
        self.view.addSubview( mUIButton )
        
        
        
        
        let searchBar = UISearchBar(frame: CGRect(x: 20, y: 100, width: 280, height: 30))
        //设置控件风格
        searchBar.searchBarStyle = UISearchBar.Style.minimal //有很多种模式minimal,prominent
        //将控件的风格修改为红色
        //searchBar.barTintColor = UIColor.gray
        //将控件背景设置为图片
        //searchBar.backgroundImage = UIImage(named: "bird2")
        searchBar.placeholder = "请输入你要输入的文字"
        //searchBar.prompt = "搜索"
        //searchBar.showsCancelButton = true //显示取消按钮
        searchBar.showsBookmarkButton = true //显示控件的书库按钮
        //searchBar.showsSearchResultsButton = true //显示控件的搜索结果按钮
        //不能和上面的2个参数同时使用
//        searchBar.setImage(UIImage(named: "bird1"), for: UISearchBar.Icon.bookmark, state: UIControl.State.normal)
        
        searchBar.showsScopeBar = true //显示控件的附件试图
        searchBar.scopeButtonTitles = ["女鞋","男鞋","内衣","秋裤"] //设置附件试图按钮的个数和标题
        searchBar.selectedScopeButtonIndex = 0 //选中附件按钮索引
        searchBar.frame = CGRect(x: 20 , y: 100, width: 280, height: 150)
        
        //附件试图的设置
        //设置附件试图按钮的背景图片
        searchBar.setScopeBarButtonBackgroundImage(UIImage(named: "bird2"), for: UIControl.State.normal)
        //设置附件试图按钮间分割线的图片
        searchBar.setScopeBarButtonDividerImage(UIImage(named: "bird3"), forLeftSegmentState: UIControl.State.normal, rightSegmentState: UIControl.State.normal)
        

        //设置附件试图按钮的标题文字属性
        searchBar.delegate = self
        //设置附件试图按钮的标题文字属性
        //let atts = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        
//        let atts = [
//            NSAttributedString.Key.font: UIFont(name:"GillSans-Bold", size:16)!,
//            NSAttributedString.Key.foregroundColor: UIColor.white
//
//        ]
        //searchBar.scopeBarButtonTitleTextAttributes(for: UIControl.State.normal)
    
        searchBar.setScopeBarButtonTitleTextAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)
        //将控件添加到当前试图
        self.view.addSubview( searchBar )


        //显示书签
        let searchBar1 = UISearchBar(frame: CGRect(x: 20, y: 250, width: UIScreen.main.bounds.width-40, height: 40))
        searchBar1.showsBookmarkButton = true  //显示书签按钮
        self.view.addSubview( searchBar1 )
        searchBar1.delegate = self
        searchBar1.placeholder = "请输入你要输入的文字"
        //光标颜色
        searchBar1.tintColor = UIColor.red
        //搜索框整个背景颜色
        searchBar1.barTintColor = UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 1.0)
        
        //搜索条背景颜色
        let seachTextFeild = searchBar1.subviews.first?.subviews.last
        seachTextFeild?.backgroundColor = UIColor.blue
        
        //搜索输入框内背景颜色
        let seachTextFeildContent = searchBar1.subviews.first?.subviews.last?.subviews.first
        seachTextFeildContent?.backgroundColor = UIColor.white
        
    
        //searchBar1.searchFieldBackgroundPositionAdjustment = UIOffset.init(horizontal: 100, vertical: 10)
        //内容偏移
        searchBar1.searchTextPositionAdjustment  = UIOffset.init(horizontal: 10, vertical: 0)
        
        /**
         .prominent  显示背景，常用在my Mail, Messages and Contacts
         .minimal  不显示背景，系统自带的背景色无效，自定义的有效，常用在Calendar, Notes and Music
         .default 默认
         */
        searchBar1.searchBarStyle = .minimal
        
        searchBar1.showsScopeBar = true
        searchBar1.scopeButtonTitles = ["One", "Two", "Three"]
        searchBar1.selectedScopeButtonIndex = 1
        
        
        
        
        //显示书签
        let searchBar2 = UISearchBar(frame: CGRect(x: 20, y: 400, width: UIScreen.main.bounds.width-40, height: 40))
        searchBar2.showsBookmarkButton = true  //显示书签按钮
        self.view.addSubview( searchBar2 )
        searchBar2.delegate = self
        searchBar2.placeholder = "请输入你要输入的文字"
        //光标颜色
        searchBar2.tintColor = UIColor.red
        //搜索框整个背景颜色
        searchBar2.barTintColor = UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 1.0)
        
        //搜索条背景颜色
        let seachTextFeild1 = searchBar2.subviews.first?.subviews.last
        seachTextFeild1?.backgroundColor = UIColor.blue
        
        //搜索输入框内背景颜色
        let seachTextFeildContent1 = searchBar2.subviews.first?.subviews.last?.subviews.first
        seachTextFeildContent1?.backgroundColor = UIColor.white
        
    
        //searchBar1.searchFieldBackgroundPositionAdjustment = UIOffset.init(horizontal: 100, vertical: 10)
        //内容偏移
        searchBar2.searchTextPositionAdjustment  = UIOffset.init(horizontal: 10, vertical: 0)
        
        /**
         .prominent  显示背景，常用在my Mail, Messages and Contacts
         .minimal  不显示背景，系统自带的背景色无效，自定义的有效，常用在Calendar, Notes and Music
         .default 默认
         */
        searchBar2.searchBarStyle = .minimal
        
        searchBar2.showsScopeBar = true
        searchBar2.scopeButtonTitles = ["One", "Two", "Three"]
        searchBar2.selectedScopeButtonIndex = 1
        
    
        //设置搜索图标
        searchBar2.setImage(UIImage.init(named: "bird2"), for: UISearchBar.Icon.search, state: .normal)
    
        //设置搜索结果图标
        searchBar2.showsSearchResultsButton = true
        searchBar2.setImage(UIImage.init(named: "bird3"), for: UISearchBar.Icon.resultsList, state: .normal)
        
        
        //背景颜色黄色
        searchBar2.scopeBarBackgroundImage = self.creatImageWithColor(color: UIColor.yellow, size: CGSize.init(width: 10, height: 10))
        
        //选中和默认字体颜色/大小
        searchBar2.setScopeBarButtonTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20), NSAttributedString.Key.foregroundColor : UIColor.red], for: .normal)
        searchBar2.setScopeBarButtonTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24), NSAttributedString.Key.foregroundColor : UIColor.yellow], for: .selected)
        
        
        
        //设置背景scopeButton 背景
        searchBar2.setScopeBarButtonBackgroundImage(creatImageWithColor(color:.orange, size: CGSize.init(width: 20, height: 20)), for: .normal)
        
        //设置附件试图按钮间分割线的图片
        searchBar2.setScopeBarButtonDividerImage(creatImageWithColor(color:.blue, size: CGSize.init(width: 20, height: 20)), forLeftSegmentState: UIControl.State.normal, rightSegmentState: UIControl.State.normal)
        
        
        
        // Do any additional setup after loading the view.
        let button12 = UIButton(type: UIButton.ButtonType.system)
        button12.frame = CGRect(x: 10, y: 500, width: 150, height: 30)
        button12.setTitle("搜索", for: UIControl.State.normal)
        button12.addTarget(self, action: #selector(JumpSearchViewControl), for: .touchUpInside)
        self.view.addSubview( button12 )
        
        
        let waveBg = self.creatImageWithColor(color: UIColor.gray, size: CGSize.init(width: 10, height: 10))
        
        let mUIImageView = UIImageView(image:  waveBg )
        mUIImageView.frame = CGRect(x: 0, y: 500, width: self.view.bounds.width, height: 200)
        
        self.view.addSubview( mUIImageView )
        
        
        drawLine(from:CGPoint(x: 10.0, y: 10.0),
                 to:CGPoint(x: 40.0, y: 100.0),imageView:mUIImageView )
        
        drawLine(from:CGPoint(x: 40.0, y: 100.0),
                 to:CGPoint(x: 50.0, y: 150.0),imageView:mUIImageView )
        drawLine(from:CGPoint(x: 50.0, y: 150.0),
                 to:CGPoint(x: 70.0, y: 180.0),imageView:mUIImageView )
        
        drawLine(from:CGPoint(x: 70.0, y: 180.0),
                 to:CGPoint(x: 90.0, y: 100.0),imageView:mUIImageView )
        
        //Data的截取
        var  data = Data()
        data.append( Data([1,2,3,4,5,7,8]) )
    
        let jary = data.subdata(in: 1..<3  )
        
        print( jary.count )
        for i in 0..<jary.count {
            print( Int( jary[i] ) )
        }
        
        let number = 200
        let low = number & 0x0f
        print( "lo=\(low)"  )
        let hight = number>>4 & 0x0f
        print( "hight=\(hight)" )
        
        print(  hight<<4 | low )
        
        
        print( pow( 2,2 ) )
        print( log(10.0) )
        print( log2(4.0) )
        
        print( log(9.0)/log(3.0) )//3为底 9为对数的 指数值
        
        var send1: Int = 199
        var data1: Data = Data(bytes: &send1, count: 8)

        var bytes = [UInt8](data1)


        print(  hw_getInt( bytes ) )
        
        print( UINT16_MAX )
        print(  )
        
    }
    //byte转int
    func hw_getInt(_ array:[UInt8]) -> Int {
        var value : UInt32 = 0
        let data = NSData(bytes: array, length: array.count)
        data.getBytes(&value, length: array.count)
        value = UInt32(bigEndian: value)
        return Int(value)
    }
    func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint, imageView:UIImageView) {
        UIGraphicsBeginImageContextWithOptions(CGSize(width: CGFloat(imageView.bounds.size.width), height: CGFloat(imageView.bounds.size.height)), false, 0)

        imageView.image?.draw(in: CGRect(x: 0, y: 0, width: imageView.bounds.size.width, height: imageView.bounds.size.height))
        
        print( imageView.bounds.size.height )
        print( imageView.bounds.size.width )

        let context = UIGraphicsGetCurrentContext()

        context?.move(to: fromPoint)
        context?.addLine(to: toPoint)

        context?.setLineCap(CGLineCap.round)
        context?.setLineWidth(1)
        context?.setStrokeColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        context?.setBlendMode(CGBlendMode.normal)
        context?.strokePath()

        imageView.image = UIGraphicsGetImageFromCurrentImageContext()
        imageView.alpha = 1.0
        UIGraphicsEndImageContext()
    }
    
    @objc func JumpSearchViewControl(){
        self.present(  UISearchController2() , animated: true, completion: nil)
    }
    
    
    func creatImageWithColor(color:UIColor,size:CGSize )->UIImage{
        let rect = CGRect(x:0,y:0,width:size.width,height:size.height)
            UIGraphicsBeginImageContext(rect.size)
            let context = UIGraphicsGetCurrentContext()
            context?.setFillColor(color.cgColor)
            context!.fill(rect)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image!
        }
    
    func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
        print( "点击书库了" )
    }
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        print( "点击清空按钮了" )
    }
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        print( "用户点击搜索栏附件试图上的按钮后触发的方法" )
    }
    
    //返回上一页
    @objc func back(){
        print("返回上一页")
        self.dismiss(animated: true,completion: nil)
    }
    
    
}
